<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'bas:mold:basMold:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="80%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsBasMoldBasMoldForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { BasMold, basMoldSave, basMoldForm } from '/@/api/bas/mold/basMold';
  import { basMoldClsTreeData } from '/@/api/bas/mold/basMoldCls';
  import { operTreeData } from '/@/api/bas/oper';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('bas.mold.basMold');
  const { showMessage } = useMessage();
  const record = ref<BasMold>({} as BasMold);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增工装档案') : t('编辑工装档案'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('工装编码'),
      field: 'code',
      component: 'Input',
      componentProps: {
        maxlength: -1,
      },
      required: true,
    },
    {
      label: t('工装尾号'),
      field: 'lastNum',
      component: 'Input',
      componentProps: {
        maxlength: -1,
      },
      // required: true,
    },
    {
      label: t('工装名称'),
      field: 'name',
      component: 'Input',
      componentProps: {
        maxlength: -1,
      },
      required: true,
    },
    {
      label: t('工装代码'),
      field: 'caddcode',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
    },
    {
      label: t('工装状态'),
      field: 'moldStatus',
      component: 'Select',
      componentProps: {
        dictType: 'bas_mold_status',
      },
      required: true,
    },
    {
      label: t('工装类型'),
      field: 'clsCode',
      fieldLabel: 'basMoldCls.name',
      component: 'TreeSelect',
      componentProps: {
        api: basMoldClsTreeData,
        allowClear: true,
      },
    },
    {
      label: t('是否共模'),
      field: 'bcomomMode',
      component: 'RadioGroup',
      componentProps: {
        dictType: 'sys_yes_no',
      },
      // required: true,
    },
    {
      label: t('存货名称'),
      field: 'basInv.cinvcode',
      fieldLabel: 'basInv.cinvname',
      component: 'ListSelect',
      componentProps: {
        selectType: 'invSelect',
      },
      // required: true,
    },
    {
      label: t('存货编码'),
      field: 'basInv.cinvcode',
      component: 'Input',
      componentProps: {
        disabled: true,
      },
      // required: true,
    },
    {
      label: t('工序'),
      field: 'operCode',
      fieldLabel: 'basOper.operName',
      component: 'TreeSelect',
      componentProps: {
        api: operTreeData,
        allowClear: true,
      },
      // required: true,
    },
    {
      label: t('位置编码'),
      field: 'locationCode',
      component: 'Input',
      componentProps: {
        maxlength: 64,
      },
    },
    {
      label: t('位置名称'),
      field: 'locationName',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
    },
    {
      label: t('部门编码'),
      field: 'depCode',
      component: 'Input',
      componentProps: {
        maxlength: 64,
      },
    },
    {
      label: t('部门名称'),
      field: 'depName',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
    },
    {
      label: t('供应商名称'),
      field: 'venName',
      component: 'Input',
      componentProps: {
        maxlength: 500,
      },
    },
    {
      label: t('购买日期'),
      field: 'buyDate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
        showTime: false,
      },
      // required: true,
    },
    {
      label: t('制作日期'),
      field: 'makeDate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
        showTime: false,
      },
    },
    {
      label: t('安装日期'),
      field: 'installDate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
        showTime: false,
      },
    },
    {
      label: t('验收日期'),
      field: 'checkDate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
        showTime: false,
      },
    },
    {
      label: t('使用日期'),
      field: 'useDate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
        showTime: false,
      },
      // required: true,
    },
    {
      label: t('保修截止日期'),
      field: 'expiryDate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
        showTime: false,
      },
    },
    // 理论次数
    {
      label: t('设计寿命'),
      field: 'iquantity',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      // rules: [{ required: true }, { pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('可用次数'),
      field: 'iqty',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      // rules: [{ required: true }, { pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    // 加工次数
    {
      label: t('累计加工次数'),
      field: 'useQty',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      // rules: [{ required: true }, { pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('追加次数'),
      field: 'addQty',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    // 保养预警次数
    {
      label: t('保养周期'),
      field: 'byQty',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('加工系数'),
      field: 'gzxs',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
      //required: true,
      // colProps: { lg: 24, md: 24 },
    },
    {
      label: t('设备名称'),
      field: 'devName',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
      //required: true,
      // colProps: { lg: 24, md: 24 },
    },
    {
      label: t('备注信息'),
      field: 'remarks',
      component: 'InputTextArea',
      componentProps: {
        maxlength: 500,
        rows: 5,
      },
      colProps: { lg: 24, md: 24 },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 8, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await basMoldForm(data);
    record.value = (res.basMold || {}) as BasMold;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      // console.log('submit', params, data, record);
      const res = await basMoldSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
